package com.king.designpatterns.hybriddesignpattern.hybriddesignpattern.observingtheintermediarypatterns;


import java.util.Observable;

/**
 * 产品事件类
 * 
 * @author king
 * @date 2021-06-14 22:35 
 */
public class ProductEvent extends Observable {
    /**
     * 事件源
     */
    private Product source;
    /**
     * 事件类型
     */
    private ProductEventType type;

    /**
     * 构造函数
     */
    public ProductEvent(Product p){
        this(p, ProductEventType.NEW_PRODUCT);
    }
    public ProductEvent(Product p, ProductEventType type){
        this.source = p;
        this.type = type;
        // 事件触发
        notifyEventDispatch();
    }

    /**
     * 获得事件源
     */
    public Product getSource() {
        return source;
    }

    /**
     * 获得事件类型
     */
    public ProductEventType getType() {
        return type;
    }

    /**
     * 触发事件处理
     */
    private void notifyEventDispatch() {
        super.addObserver(EventDispatch.getEventDispatch());
        super.setChanged();
        super.notifyObservers(source);
    }
}
